{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PHydro-cover-small.png\">\n",
    "*This is the Jupyter notebook version of the [Python in Hydrology](http://www.greenteapress.com/pythonhydro/pythonhydro.html) by Sat Kumar Tomer.*\n",
    "*Source code is available at [code.google.com](https://code.google.com/archive/p/python-in-hydrology/source).*\n",
    "\n",
    "*The book is available under the [GNU Free Documentation License](http://www.gnu.org/copyleft/fdl.html). If you have comments, corrections or suggestions, please send email to satkumartomer@gmail.com.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [KS Test](05.04-KS-Test.ipynb) | [Contents](Index.ipynb) | [Measure of Statistical Dependence](05.06-Measure-of-Statistical-Dependence.ipynb) >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.5 卡方检验\n",
    "\n",
    "我们也可以通过对比它们的PDF比较分布。在这种情况下，我们使用卡方($\\chi^2$)检验。在这个检验中，首先计算$\\chi^2$统计量，基于此我们说分布相同与否。我们可以比较样本与理论分布，或两个样本。我们将采用两个pdf，其中一个被认为是观观察到的，另一个是预期的。我们将使用`scipy.stats.mstats`库中的`chisquare`函数。这个函数给出了测试的$\\chi^2$统计量和p-value。在下面的例子中，我们得到的一个接近零的p-value，这意味着这两个频率来自不同的分布。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25.0 1.54404982911e-05\n"
     ]
    }
   ],
   "source": [
    "from scipy.stats.mstats import chisquare\n",
    "import numpy as np\n",
    "f_obs = np.array([10,15,20,20]) #观测的pdf\n",
    "f_exp = np.array([10,5,15,30 #期望的pdf\n",
    "# 卡方检验\n",
    "c,p = chisquare(f_obs,f_exp)\n",
    "\n",
    "print(c,p)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
